package com.wuseguang.springcloud.dao.jdbc;

import com.alibaba.fastjson.JSON;
import com.wuseguang.springcloud.JdbcTemplate.BaseTemplate;
import com.wuseguang.springcloud.entity.ClassDutyList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class ClassDutyJdbc extends BaseTemplate {

    private Logger logger = LoggerFactory.getLogger(this.getClass());


    public List<Map<String, Object>> batchaddClassDuty(int school_id, ClassDutyList classDutyList, List<Map<String, Object>> lists) {
        String sql = "insert into `class_duty_time` (`projects`, `groups`, `school_id`, `class_id`, `starttime`, `type`, `index`) values(?,?,?,?,?,?,?)";
        String sql1 = "DELETE FROM class_duty_time WHERE school_id=? AND class_id=?";
        String sql2 = "DELETE FROM class_duty WHERE school_id=? AND class_id=? ";
        String sql3 = "insert into `class_duty` (`student_id`, `x`, `y`, `school_id`, `class_id`) values(?,?,?,?,?)";
        List<Map<String, Object>> classDuties = new ArrayList<Map<String, Object>>();
        try {
            Integer classid = classDutyList.getClassid();
            String groups = classDutyList.getGroups();
            Integer index = classDutyList.getIndex();
            String projects = classDutyList.getProjects();
            Integer type = classDutyList.getType();
            String starttime = classDutyList.getStarttime();
            updateByParameter(sql1, school_id, classid);
            updateByParameter(sql, projects, groups, school_id, classid, starttime, type, index);
            updateByParameter(sql2, school_id, classid);
            String dutyList = classDutyList.getList();
            if (dutyList != null&&lists!=null&&lists.size()>0) {
                List<Map<String, Object>> list = (List<Map<String, Object>>) JSON.parse(dutyList);
                List<Map<String, Object>> maps = new ArrayList<>();
                logger.info("JSON==>list={}", list.toString());
                for (int i = 0; i < list.size(); i++) {
                    Map<String, Object> map1 = list.get(i);
                    if (map1 != null && map1.size() > 0) {
                        int a = 0;
                        String name = (String) map1.get("name");
                        for (int j = 0; j < lists.size(); j++) {
                            Map<String, Object> map = lists.get(j);
                            String name1 = (String) map.get("name");
                            int id = (int) map.get("id");
                            if (name != null && name.trim().equals(name1)) {
                                a++;
                                map1.put("id", id);
                                maps.add(map1);
                                break;
                            }
                            if ("".equals(name) || name == null) {//输入空字符串
                                a++;
                                map1.put("id", 0);
                                maps.add(map1);
                                break;
                            }
                        }
                        if (a == 0) {
                            classDuties.add(map1);
                        }
                    }
                    logger.info("map1={}", map1.toString());
                }
                if (classDuties != null && classDuties.size() > 0) {
                    return classDuties;
                }
                if (maps != null && maps.size() > 0) {
                    dao.batchUpdate(sql3, new BatchPreparedStatementSetter() {
                        @Override
                        public void setValues(PreparedStatement ps, int i) throws SQLException {
                            Map<String, Object> map = maps.get(i);
                            logger.info("batchUpDateClass()={}", map.toString());
                            Integer id = (Integer) map.get("id");
                            Integer x = (Integer) map.get("x");
                            Integer y = (Integer) map.get("y");
                            ps.setInt(1, id);
                            ps.setInt(2, x);
                            ps.setInt(3, y);
                            ps.setInt(4, school_id);
                            ps.setInt(5, classid);
                        }

                        @Override
                        public int getBatchSize() {
                            return maps.size();
                        }
                    });
                }
            }
        } catch (Exception e) {
            logger.info("批量添加异常,error={}", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();// 手动回滚
        }
        return classDuties;
    }
}